package bupt.party.online.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import bupt.party.online.dao.IBranchNewsDao;
import bupt.party.online.model.BranchNews;
import bupt.party.online.model.PartyNews;
import bupt.party.online.service.IBranchNewsService;
public class BranchNewsServiceImpl implements IBranchNewsService{
	
	IBranchNewsDao branchNewsDao;
	
	public IBranchNewsDao getBranchNewsDao() {
		return branchNewsDao;
	}

	public void setBranchNewsDao(IBranchNewsDao branchNewsDao) {
		this.branchNewsDao = branchNewsDao;
	}

	public BranchNews findBranchNewsById(int id) {
		//根据id查找支部新闻
		return branchNewsDao.findById(id);
	}

	public List<BranchNews> findAllBranchNews() {
		// 查找所有支部新闻
		return branchNewsDao.queryAll();
	}

	public int getResultCountByTag(int tag) {
		// TODO Auto-generated method stub

		if(tag <= 0) return 0;
		
		else return this.branchNewsDao.getResultCountByTag(tag);
	}

	public List<BranchNews> getNewsByCategory(int cateTag, int pageNow,
			int pageSize) {
		// TODO Auto-generated method stub
		if(cateTag <= 0) return null;
		
		int firstResult = pageSize*(pageNow - 1);
		
		return branchNewsDao.getResultsByPage(cateTag, firstResult, pageSize);
		
	}

	@Override
	public int getResultCountByConditions(Map<String, Object> conditions) {
		// TODO Auto-generated method stub
		
		String countSql = "select count(*) from BranchNews ";
		
		if(conditions == null || conditions.size() <= 0) return this.branchNewsDao.getResultCount(countSql,null);
		
		Object[] queryValues = new Object[conditions.size()];
		
		Iterator it = conditions.entrySet().iterator();
		
		int i=0;
		
		while(it.hasNext()){
			
			Entry entry = (Entry) it.next();
			
			queryValues[i] = entry.getValue();
			
			if(0==i){
				
				if(entry.getKey().equals("title")) countSql += " where "+entry.getKey()+" like ? ";
				
				else countSql += " where "+entry.getKey()+"=? ";
				
				i++;
				
				continue;
			} 
			
			if(entry.getKey().equals("title")) countSql +=" and "+entry.getKey()+" like ? ";
				
			else countSql +=" and "+entry.getKey()+"=? ";
			
			i++;
			
		}
		
		return this.branchNewsDao.getResultCount(countSql,queryValues);
	}
	@Override
	public List<BranchNews> getNewsByConditions(Map<String,Object> conditions,int pageNow,int pageSize) {
		// TODO Auto-generated method stub
		
		String queryString = "from BranchNews ";
		
		int firstResult = pageSize*(pageNow -1);
		
		if(firstResult < 0) firstResult = 0;
		
		if(conditions == null || conditions.size() <= 0) return this.branchNewsDao.QueryByConditions(queryString,null,firstResult,pageSize);
		
		Object[] queryValues = new Object[conditions.size()];
		
		Iterator it = conditions.entrySet().iterator();
		
		int i=0;
		
		while(it.hasNext()){
			
			Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
			
			queryValues[i] = entry.getValue();
			
			if(0==i){
				
				if(entry.getKey().equals("title")) queryString += " where "+entry.getKey()+" like ? ";
				
				else queryString += " where "+entry.getKey()+"=? ";
				
				i++;
				
				continue;
			} 
			
			if(entry.getKey().equals("title")) queryString +=" and "+entry.getKey()+" like ? ";
			
			else queryString +=" and "+entry.getKey()+"=? ";
			
			i++;
			
		}
		
		return this.branchNewsDao.QueryByConditions(queryString,queryValues,firstResult,pageSize);
	}

	@Override
	public boolean saveOrUpdate(BranchNews bean) {
		// TODO Auto-generated method stub
		
		try{
			this.branchNewsDao.saveOrUpdate(bean);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public BranchNews getNewsById(int id) {
		// TODO Auto-generated method stub
		if(id < 0) id = 0;
		
		BranchNews result = branchNewsDao.findById(id);
		
		return result;
	}

	@Override
	public boolean deleteNewsById(int id) {
		// TODO Auto-generated method stub
		
		if(id < 0) return false;
		
		try{
			return this.branchNewsDao.deleteNewsById(id);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

}
